package org.swan.web.user.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.stream.base.MessageBean;
import org.swan.web.user.bean.UserRoleBean;
import org.swan.web.user.service.UserRoleService;

import java.util.List;

@RestController
@RequestMapping("/user_role")
public class UserRoleController {
    private final UserRoleService userRoleService;

    public UserRoleController(UserRoleService userRoleService) {
        this.userRoleService = userRoleService;
    }

    @GetMapping("/get")
    public MessageBean<UserRoleBean> get(@RequestParam("userRoleId") long userRoleId) {
        return MessageBean.MSG_SUCCESS.setData(userRoleService.getUserRoleById(userRoleId));
    }

    @PostMapping("add")
    public MessageBean<UserRoleBean> add(@RequestBody UserRoleBean userRoleBean) {
        long userRoleId = userRoleService.addUserRole(userRoleBean);
        return MessageBean.MSG_SUCCESS.setData(userRoleService.getUserRoleById(userRoleBean.getUserRoleId()));
    }

    @PostMapping("update")
    public MessageBean<UserRoleBean> update(@RequestBody UserRoleBean userRoleBean) {
        return MessageBean.MSG_SUCCESS.setData(userRoleService.updateUserRole(userRoleBean));
    }

    @GetMapping("delete")
    public MessageBean<UserRoleBean> delete(@RequestParam("userRoleId") long userRoleId) {
        return MessageBean.MSG_SUCCESS.setData(userRoleService.deleteUserRoleById(userRoleId));
    }

    @GetMapping("list")
    public MessageBean<List<UserRoleBean>> list(@Param("offset") int offset, @Param("rows") int rows) {
        return MessageBean.MSG_SUCCESS.setData(userRoleService.list(offset, rows));
    }
}
